using System.Collections.Generic;
using UFrame.MapJson;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace UFrame
{
    public class MapMainInterface : EditorWindow
    {
        private string[] commondTexts = { "选择svn操作..", "更新", "提交", "还原", "cleanup", "log" };

        static MapMainInterface windows = null;

        List<string> id = new List<string>();
        string[] array = new string[0];
        int index = 0;
        string selectId = string.Empty;

        [MenuItem("Plateface/Map/MainInterface &mp")]
        public static void OpenMapEditorWindows()
        {
            if (windows == null)
            {
                windows = GetWindowWithRect<MapMainInterface>(new Rect(0, 0, 600, 400), false, "主界面", true);
                windows.Show();
            }
        }

        public MapMainInterface()
        {
        }

        void OnEnable()
        {
            this.Load();
        }


        /// <summary>
        /// 本对象销毁
        /// </summary>
        public void OnDestroy()
        {
            OnClose();
        }

        public void OnGUI()
        {
            GUI.backgroundColor = Color.gray;
            GUI.color = Color.white;
            GUILayout.Label(
                "-----------------------------------------------------------------------------------------------------------------------------");
            EditorGUILayout.BeginVertical();
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("Npc", GUILayout.Height(23)))
            {
                NpcEditor.OpenWindow();
            }
            else if (GUILayout.Button("Monster", GUILayout.Height(23)))
            {
                MonsterEditor.OpenWindow();
            }
            else if (GUILayout.Button("采集物", GUILayout.Height(23)))
            {
                CollectEditor.OpenWindow();
            }
            else if (GUILayout.Button("副职业采集物", GUILayout.Height(23)))
            {
                CollectionEditor.OpenWindow();
            }
            else if (GUILayout.Button("传送门", GUILayout.Height(23)))
            {
                PortalEditor.OpenWindow();
            }
            else if (GUILayout.Button("安全区域", GUILayout.Height(23)))
            {
                SafeAreaEditor.OpenWindow();
            }
            else if (GUILayout.Button("事件触发", GUILayout.Height(23)))
            {
                TriggerEventEditor.OpenWindow();
            }

            GUI.color = Color.green;
            if (GUILayout.Button("保存", GUILayout.Height(32)))
            {
                //                OnClose();
                Save();
            }

            GUI.color = Color.white;

            EditorGUILayout.EndVertical();
            GUILayout.Label(
                "------------------------------------------------------------------------------------------------------------------------------");

            this.Repaint();
        }

        /// <summary>
        /// 加载
        /// </summary>
        void Load()
        {
        }

        /// <summary>
        /// 保存
        /// </summary>
        void Save()
        {
            /*string sceneName = LoadSceneGroup.instance.Get(m_mapInfo.mapID);
            if (sceneName != null && sceneName != GetCurrentSceneName())
            {
                SaveHint.SaveHintWindow(string.Format("MapID{0}已被{1}场景使用,是否保存？", m_mapInfo.mapID, sceneName), null, ()=>{
                    LoadSceneGroup.instance.Remove(m_mapInfo.mapID.ToString());
  
                    LoadSceneGroup.instance.Add(m_mapInfo.mapID.ToString(), GetCurrentSceneName());
                    m_id = LoadSceneGroup.instance.Get(GetCurrentSceneName());
                    LoadSceneGroup.instance.Save();
                    DataManagerInstance.instance.Save(PathMake.XMLPath(m_mapInfo.mapID.ToString()));
                    DataManagerInstance.instance.SaveJson( PathMake.MapJsonPath( m_mapInfo.mapID.ToString() ) );
                } );
            }
            else
            {
                LoadSceneGroup.instance.Add(m_mapInfo.mapID.ToString(), GetCurrentSceneName());
                m_id = LoadSceneGroup.instance.Get(GetCurrentSceneName());
                LoadSceneGroup.instance.Save();
                DataManagerInstance.instance.Save(PathMake.XMLPath(m_mapInfo.mapID.ToString()));
                DataManagerInstance.instance.SaveJson( PathMake.MapJsonPath( m_mapInfo.mapID.ToString() ) );
            }*/
        }

        /// <summary>
        /// 关闭所有子窗口
        /// </summary>
        void OnClose()
        {
            //NavMeshEditor.CloseWindow();
            NpcEditor.CloseWindow();
            MonsterEditor.CloseWindow();
            PortalEditor.CloseWindow();
            SafeAreaEditor.CloseWindow();
            TriggerEventEditor.CloseWindow();
            //CollectEditor.CloseWindow();
            //CollectionEditor.CloseWindow();
        }

        /// <summary>
        /// 得到当前场景名
        /// </summary>
        string GetCurrentSceneName()
        {
            var v = EditorSceneManager.GetActiveScene();
            if (v == null)
            {
                return string.Empty;
            }

            string name = SceneManager.GetActiveScene().name;
            if (string.IsNullOrEmpty(name))
            {
                return "";
            }

            string[] array = name.Replace(".unity", "").Split('/');
            return array[array.Length - 1];
        }
    }
}